﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MsDAL;
using Model;
using Components;

namespace UIMarker
{
    public partial class FrmSys5a00Slt : Form
    {
        string insertTmp = @"INSERT INTO Sys5a00(listid,pagename,ctlname,colsize,pagetitle,lsttime,remark) VALUES
('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}');";
        string updateTmp = @"UPDATE Sys5a00 SET [pagename] = '{1}',
[ctlname] = '{2}',
[colsize] = '{3}',
[pagetitle] = '{4}',
[lsttime] = '{5}',
[remark] = '{6}',
WHERE [listid] = '{0}'; ";

        Sys5a00 model = null;

        Sys5a00DAL dal = new Sys5a00DAL();
        CommonDAL comdal = new CommonDAL();
        public FrmSys5a00Slt()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            DataTable dt = dal.GetPageList();
            this.dgvMain.DataSource = dt;
        }

        private void dgvMain_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.dgvMain.SelectedRows.Count == 0)
            {
                MessageBox.Show("必须选中行!");
                return;
            }

            DataGridViewRow row = this.dgvMain.SelectedRows[0];

            model = new Sys5a00();
            UIHelper.FillModelFromDataGridViewRow(model, row);

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        public Sys5a00 GetSelectedData()
        {
            return model;
        }

        private void dgvMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) //回车选定数据
            {
                dgvMain_MouseDoubleClick(null, null);
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (this.dgvMain.SelectedRows.Count == 0)
            {
                MessageBox.Show("必须选中行!");
                return;
            }

            ToolStripButton s = sender as ToolStripButton;

            DataGridViewRow row = this.dgvMain.SelectedRows[0];

            model = new Sys5a00();
            UIHelper.FillModelFromDataGridViewRow(model, row);

            string temp = s == toolStripButton3 ? insertTmp : updateTmp;

            string cnt = string.Format(temp,
                model.Listid,model.Pagename, model.Ctlname, model.Colsize, model.Pagetitle, model.Lsttime, model.Remark);

            //将内容复制到剪切板中
            Clipboard.SetDataObject(cnt);

            model = null;
        }
    }
}
